package com.atguigu.yygh.handler;

import com.atguigu.yygh.exception.YYGHException;
import com.atguigu.yygh.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/*==========================================
      时 间:   2022-04-13
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
@RestControllerAdvice //全局异常处理类
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(value = {Exception.class}) //统一异常处理
    public R handleAllException(Exception ex){
        ex.printStackTrace();
       log.error(ex.getMessage());
       return R.error();
    }

    @ExceptionHandler(value = {ArithmeticException.class}) //特定异常处理
    public R handleArithmeticException(ArithmeticException ex){
        log.error(ex.getMessage());
        return R.error().message("数学异常");
    }
    @ExceptionHandler(value = {SQLException.class}) //特定异常处理
    public R handleSQLException(SQLException ex){
        log.error(ex.getMessage());
        return R.error().message("数学异常");
    }


    @ExceptionHandler(value = {YYGHException.class}) //特定异常处理
    public R handleSQLException(YYGHException ex){
        log.error(ex.getMessage());
        return R.error().message(ex.getMessage()).code(ex.getCode());
    }

}
